package com.zy.qudadid.ui.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;
import com.zy.lcpassenger.R;
import com.zy.qudadid.model.About;
import com.zy.qudadid.network.Const;
import com.zy.qudadid.presenter.IntegralPresenter;
import com.zy.qudadid.ui.activity.base.ToolBarActivity;
import com.zy.qudadid.ui.fragment.IntegralOther1Fragment;
import com.zy.qudadid.ui.fragment.IntegralOther2Fragment;
import com.zy.qudadid.ui.fragment.IntegralOther3Fragment;
import com.zy.qudadid.ui.fragment.ShopFragment;
import com.zy.qudadid.ui.widget.LocalImageHolderView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.OnClick;
import okhttp3.Call;

/**
 * 积分商城
 */
public class IntegralActivity extends ToolBarActivity<IntegralPresenter> {

    private Fragment mTab01;
    private Fragment mTab02;
    private Fragment mTab03;
    private Fragment mTab04;

    ConvenientBanner convenientBanner;
    private List listBanner;

    @Override
    protected CharSequence provideTitle() {
        return "积分商城";
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_integral;
    }

    @Override
    public void initListeners() {

    }

    @Override
    protected void initThings(Bundle savedInstanceState) {
        super.initThings(savedInstanceState);

        convenientBanner = (ConvenientBanner) findViewById(R.id.convenientBanner);

        listBanner = new ArrayList();
        OkHttpUtils.post()
                .url(Const.BASE_URL+"api.php/Shop/slide")
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int i) {

                    }

                    @Override
                    public void onResponse(String s, int i) {
                        JSONObject json;
                        try {
                            json = new JSONObject(s);
                            int code = json.getInt("code");
                            if(code == Const.OK){
                                JSONArray datas = json.getJSONArray("datas");
                                ArrayList<About> list = new ArrayList<About>();
                                list = new Gson().fromJson(datas.toString(),new TypeToken<ArrayList<About>>(){}.getType());
                                for (About item :
                                        list) {
                                    listBanner.add(item.imagepath);
                                }
                            }else{
                                listBanner.add("");
                            }

                            //自定义你的Holder，实现更多复杂的界面，不一定是图片翻页，其他任何控件翻页亦可。
                            convenientBanner.setPages(
                                    new CBViewHolderCreator<LocalImageHolderView>() {
                                        @Override
                                        public LocalImageHolderView createHolder() {
                                            return new LocalImageHolderView();
                                        }
                                    }, listBanner)
                                    .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.CENTER_HORIZONTAL)
                                    //设置两个点图片作为翻页指示器，不设置则没有指示器，可以根据自己需求自行配合自己的指示器,不需要圆点指示器可用不设
                                    .setPageIndicator(new int[]{R.drawable.ic_page_indicator, R.drawable.ic_page_indicator_focused})
                                    //设置指示器的方向
                                    .startTurning(3000)
                                    .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.ALIGN_PARENT_RIGHT);
                            //设置翻页的效果，不需要翻页效果可用不设
                            //.setPageTransformer(Transformer.DefaultTransformer);    集成特效之后会有白屏现象，新版已经分离，如果要集成特效的例子可以看Demo的点击响应。
//        convenientBanner.setManualPageable(false);//设置不能手动影响
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });

        setSelect(0);


    }

    @Override
    public IntegralPresenter createPresenter() {
        return new IntegralPresenter();
    }

    @OnClick({R.id.rb_tab1,R.id.rb_tab2,R.id.rb_tab3,R.id.rb_tab4})
    public void tab(View v){
        switch (v.getId()){
            case R.id.rb_tab1:
                setSelect(0);
                break;
            case R.id.rb_tab2:
                setSelect(1);
                break;
            case R.id.rb_tab3:
                setSelect(2);
                break;
            case R.id.rb_tab4:
                setSelect(3);
                break;
        }
    }

    private void setSelect(int i){
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        hideFragment(transaction);
        // 把图片设置为亮的
        // 设置内容区域
        switch (i){
            case 0:
                if (mTab01 == null) {
                    mTab01 = new ShopFragment();//积分商城
                    transaction.add(R.id.fl_content, mTab01);
                } else {
                    transaction.show(mTab01);
                }
                break;
            case 1:
                if (mTab02 == null) {
                    mTab02 = new IntegralOther1Fragment();//吱友
                    transaction.add(R.id.fl_content, mTab02);
                } else {
                    transaction.show(mTab02);
                }
                break;
            case 2:
                if (mTab03 == null) {
                    mTab03 = new IntegralOther2Fragment();//朋友圈
                    transaction.add(R.id.fl_content, mTab03);
                } else {
                    transaction.show(mTab03);
                }
                break;
            case 3:
                if (mTab04 == null) {
                    mTab04 = new IntegralOther3Fragment();//直播
                    transaction.add(R.id.fl_content, mTab04);
                } else {
                    transaction.show(mTab04);
                }
                break;

        }
        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction)
    {
        if (mTab01 != null) {
            transaction.hide(mTab01);//吱聊
        }
        if (mTab02 != null) {
            transaction.hide(mTab02);//吱友
        }
        if (mTab03 != null) {
            transaction.hide(mTab03);//朋友圈
        }
        if (mTab04 != null) {
            transaction.hide(mTab04);//直播
        }
    }
}
